package basis;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

/**
 * @author dongyang
 * @since 2020-12-28 15:44
 */
@WebListener
public class RequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent event) {
        HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
        if (!request.getServletPath().equals("/counter")) {
            ServletContext context = event.getServletContext();
            Integer counter = 0;
            if (context.getAttribute("counter") != null) {
                counter = (Integer) context.getAttribute("counter");
            }
            context.setAttribute("counter", counter + 1);
            System.out.println(context.getAttribute("counter"));
        }
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }

    // implementations for other methods
}
